package com.icesoft.system.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.icesoft.framework.core.config.Cons;
import com.icesoft.system.entity.BaseLoginUser;
import com.icesoft.system.util.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 字段自动填充策略处理类
 */
@Component
public class AutoFillMetaObjectHandler implements MetaObjectHandler {

	@Override
	public void insertFill(MetaObject metaObject) {
		BaseLoginUser user = SecurityUtils.getCurrentUser();
		if (user != null) {
			this.setFieldValByName(Cons.FieldName.companyId.name(),user.getCompanyId(),metaObject);
		}
		this.strictInsertFill(metaObject, Cons.FieldName.createTime.name(), Date::new, Date.class);
		this.strictInsertFill(metaObject, Cons.FieldName.updateTime.name(), Date::new, Date.class);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		this.strictUpdateFill(metaObject, Cons.FieldName.updateTime.name(), Date::new, Date.class);
	}
}